var prefix = "/farm/farmShop"
$(function () {
    obj = document.getElementsByName("test");
    check_val = [];
    for(k in obj){
        if(obj[k].checked)
            check_val.push(obj[k].value);
    }
    load();
});
var address = '';
var shopType = '';
function load() {
    $('#exampleTable')
        .bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                //	showRefresh : true,
                //	showToggle : true,
                //	showColumns : true,
            //    showLoading:true,
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                //search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                queryParams: function (params) {
                    return {
                        //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset: params.offset,
                        farmName:$('#searchName').val(),
                        // username:$('#searchName').val()
                        // accommodation:$('#accommodation').val(),
                        // diningRoom:$('#diningRoom').val()
                        address: address||'',
                        beginTime: $('#beginTime').val(),
                        endTime: $('#endTime').val(),
                        shopType:shopType||''
                    };
                },
                // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [
                 /*   {
                        checkbox: true
                    },*/
                    {
                        field: 'id',
                        title: '唯一标识',
                        // 隐藏列
                        visible: false,
                        checkbox: true
                    },
                    {
                        //field: 'Number',//可不加
                        title: '序号',//标题  可不加
                        align: 'center',
                        formatter: function (value, row, index) {
                            return index+1;
                        }
                    },
                    {
                        field: 'farmName',
                        title: '名称',
                        align: 'center',
                    },
                    {
                        field: 'name',
                        title: '联系人姓名',
                        align: 'center',
                    },
                    {
                        field: 'phone',
                        title: '联系人电话',
                        align: 'center',
                    },
                    {
                        field: 'storePhotos',
                        title: '图片',
                        align: 'center',
                        width: 90,
                        formatter: function (value, row, index) {
                            var s;
                            if (row.storePhotos != null) {
                                var arr = row.storePhotos.split(',');
                                for (var i = 0; i < arr.length; i++) {
                                    var url = arr[i]
                                    s = '<a class = "view"  href="javascript:void(0)"><img style="width:60px;height:60px;"  src="' + url + '"/></a>';
                                }
                            }
                            return s;
                        },

                        events: 'operateEvents'
                    },
                    {
                        field: 'consumption',
                        title: '人均消费(元/人)',
                        align: 'center',
                    },
                    {
                        field: 'accommodation',
                        title: '住宿',
                        align: 'center',
                        formatter: function (value, row, index) {
                            if (row.accommodation == '1') {
                                return '是';
                            } else if (row.accommodation == '2') {
                                return '否';
                            }
                        }
                    },
                    {
                        field: 'diningRoom',
                        title: '美食',
                        align: 'center',
                        formatter: function (value, row, index) {
                            if (row.diningRoom == '1') {
                                return '是';
                            } else if (row.diningRoom == '2') {
                                return '否';
                            }
                        }
                    },

                    {
                        field: 'businessLicens',
                        title: '营业执照',
                        align: 'center',
                        width: 90,
                        formatter: function (value, row, index) {
                            var s;
                            if (row.businessLicens != null) {
                                var arr = row.businessLicens.split(',');
                                for (var i = 0; i < arr.length; i++) {
                                    var url = arr[i]
                                    s = '<a class = "view"  href="javascript:void(0)"><img style="width:60px;height:60px;"  src="' + url + '"/></a>';
                                }
                            }
                            return s;
                        },

                        events: 'businessEvents'
                    },
                    {
                        field: 'hotValue',
                        title: '热度',
                        align: 'center',
                    },
                    {
                        field: 'address',
                        title: '地址',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var e = '<a class=" ' + s_edit_h + '" href="#" mce_href="#" title="查看" onclick="getLocation(\''
                                + row.id
                                + '\')">' + value + '</a> ';
                            return e ;
                        }
                    },
                    {
                        field: 'recommend',
                        title: '热门农家',
                        align: 'center', valign: 'center',
                        formatter: function (value, row, index) {
                            if (row.recommend == '1') {
                                return '热门';
                            } else if (row.recommend == '2') {
                                return '-';
                            }
                        }
                    },
                    {
                        field: 'updownStatus',
                        title: '状态',
                        align: 'center', valign: 'center',
                        formatter: function (value, row, index) {
                            if (row.updownStatus == '1') {
                                return '正常';
                            } else if (row.updownStatus == '2') {
                                return '下架';
                            }
                        }
                    },
                    {
                        field: 'createTime',
                        align: 'center',
                        title: '入驻日期'
                    },
                    {
                        title: '操作',
                        field: 'none',
                        align: 'center',
                        width: 80,
                        formatter: function (value, row, index) {

                            var r = '<a><span class=" ' + s_preview + '" href="#" title="预览"  mce_href="#" onclick="preview('+ row.id + ', + '+row.updownStatus+' ,+'+row.status+')">' +
                                '<span>&nbsp;预览<span/></span></a><br/>';

                           /* var e = '<a><span class=" ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit(\''   + ',\'' +row.status
                                + row.id
                                + '\')"><span>&nbsp;编辑<span/><span/></a> ';*/
                            if(row.recommend=='1'){
                                var str = '<span>&nbsp;取消热门<span/>'
                            }else {
                              /*  str = '<span style="color: #777777">&nbsp;添加热门<span/>'*/
                                str = '<span >&nbsp;添加热门<span/>'
                            }

                            var f = '<a id="aa"><span class="" href="#" title="添加热门"  mce_href="#" onclick="addHotShop('+ row.id + ',\'' + row.recommend+'\')">'+str+'</span></a><br/>';

                            if(row.updownStatus=='1'){
                                var strStatus = '<span>&nbsp;下架<span/>'
                            }else {
                                strStatus = '<span>&nbsp;正常<span/>'
                            }
                            var d = '<a><span class="' + s_remove_h + '" href="#" title="下架"  mce_href="#" onclick="resetPwd('+ row.id + ',\'' + row.updownStatus+'\')"><span>'+strStatus+'<span/></span></a><br/>';

                            return r + f + d;
                        }
                    }]
            });
}


function addHotShop(id,recommend) {
    if (recommend==1){
      //  layer.msg("店铺已是热门");
        layer.confirm('店铺已是热门,是否将此店铺更改为普通店铺？', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                url: prefix + "/update",
                type: "post",
                data: {
                    id: id,
                    recommend: 2
                },
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        })
        return;
    }
    layer.confirm('是否将此店铺更改为热门店铺？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/update",
            type: "post",
            data: {
                id: id,
                recommend: 1
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function preview(id,updownStatus,status) {
    if (updownStatus != 1||status != 1){
        layer.msg("商铺已下架或未审核");
        return;
    }
    // 编辑页面全屏
    var index = layer.open({
        type: 2,
        title: '农家详情',
        content: prefix + '/preview/' + id, // iframe的url
        area: ['37%', '100%'],
        maxmin: false,
        closeBtn: 1
    });
    // layer.full(index);
}

function getLocation (id) {
    // 编辑页面全屏
    var index = layer.open({
        type: 2,
        title: '地图',
        content: prefix + '/getLocation/' + id, // iframe的url
        area: ['300px', '195px'],
        maxmin: false,
        closeBtn: 1
    });
    layer.full(index);
}

function reLoad() {
    // obj = document.getElementsByName("test");
    // check_val = [];
    // for(k in obj){
    //     if(obj[k].checked)
    //         check_val.push(obj[k].value);
    // }
   // alert(check_val);
    address = ''
    var check =$('#test').val()
    shopType =$('#shopType').val()
    // for (var i = 0; i < check.length; i++) {
    //     if (check[0]=='') {
    //         break;
    //     }
    //     address +=check[i]+','
    // }
    address = check
    // $("#test").hide();
    $('#exampleTable').bootstrapTable('refresh');
}

function add() {
    layer.open({
        type: 2,
        title: '增加',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: prefix + '/add' // iframe的url
    });
}

function edit(id) {
// 编辑页面全屏
    var index = layer.open({
        type: 2,
        title: '编辑',
        content: prefix + '/edit/' + id, // iframe的url
        area: ['300px', '195px'],
        maxmin: false,
        closeBtn: 1
    });
    layer.full(index);

}

function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'id': id
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}


// 点击之后放大
window.operateEvents = {
    'click .view': function (e, value, row, index) {
        console.log(row);
        var imageUrl = row.storePhotos;
        var arr = imageUrl.split(',');
        layer.open({
            type: 2,
            title: false,
            // maxmin: true,
            shadeClose: true, // 点击遮罩关闭层
            area: ['600px', '380px'],
            content: "/farm/scenic/preview?imgUrl="+arr[0]+"&imgUrls="+imageUrl
        });
    }
}

// 点击之后放大
window.businessEvents = {
    'click .view': function (e, value, row, index) {
        console.log(row);
        var imageUrl = row.businessLicens;
        var arr = imageUrl.split(',');
        layer.open({
            type: 2,
            title: false,
            // maxmin: true,
            shadeClose: true, // 点击遮罩关闭层
            area: ['600px', '380px'],
            content: "/farm/scenic/preview?imgUrl="+arr[0]+"&imgUrls="+imageUrl
        });
    }

}

// 下架
function resetPwd(id ,updownStatus) {
    if (updownStatus==2){
        //  layer.msg("店铺已是热门");
        layer.confirm('店铺已下架,是否将此店铺更改为正常状态？', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                url: prefix + "/update",
                type: "post",
                data: {
                    id: id,
                    updownStatus: 1
                },
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        })
        return;
    }
    layer.confirm('确定要下架该店铺？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/update",
            type: "post",
            data: {
                id: id,
                updownStatus: 2
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function batchRemove() {
    var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0) {
        layer.msg("请选择要删除的数据");
        return;
    }
    layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        var ids = new Array();
        // 遍历所有选择的行数据，取每条数据对应的ID
        $.each(rows, function (i, row) {
            ids[i] = row['id'];
        });
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids
            },
            url: prefix + '/batchRemove',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }, function () {

    });
}

// 点击之后放大
window.operateEvents = {
    'click .view': function (e, value, row, index) {
        console.log(row);
        var imageUrl = row.businessLicens;
        var arr = imageUrl.split(',');
        layer.open({
            type: 2,
            title: false,
            // maxmin: true,
            shadeClose: true, // 点击遮罩关闭层
            area: ['600px', '380px'],
            content: "/farm/scenic/preview?imgUrl="+arr[0]+"&imgUrls="+imageUrl
        });
    }
}

// 点击之后放大
window.operateEvents = {
    'click .view': function (e, value, row, index) {
        console.log(row);
        var imageUrl = row.storePhotos;
        var arr = imageUrl.split(',');
        layer.open({
            type: 2,
            title: false,
            // maxmin: true,
            shadeClose: true, // 点击遮罩关闭层
            area: ['600px', '380px'],
            content: "/farm/scenic/preview?imgUrl="+arr[0]+"&imgUrls="+imageUrl
        });
    }
}